package com.ksm.zyz.common.download;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.ksm.zyz.common.util.DateUtil;

 
public class ExportUtil {
 
    public static void ExportExcel(String[] titles, ArrayList<Object[]> list,HttpServletResponse response) throws FileNotFoundException, IOException{
        // 创建一个workbook 对应一个excel应用文件
        XSSFWorkbook workBook = new XSSFWorkbook();
        // 在workbook中添加一个sheet,对应Excel文件中的sheet
        //Sheet名称，可以自定义中文名称
        XSSFSheet sheet = workBook.createSheet("Sheet1");
        ExportInternalUtil exportUtil = new ExportInternalUtil(workBook, sheet);
        XSSFCellStyle headStyle = exportUtil.getHeadStyle();
        XSSFCellStyle bodyStyle = exportUtil.getBodyStyle();
        // 构建表头
        XSSFRow headRow = sheet.createRow(0);
        XSSFCell cell = null;
 
        // 输出标题
        for (int i = 0; i < titles.length; i++) {
            cell = headRow.createCell(i);
            cell.setCellStyle(headStyle);
            cell.setCellValue(titles[i]);
        }
        // 构建表体数据
        for (int j = 0; j < list.size(); j++) {
            XSSFRow bodyRow = sheet.createRow(j + 1);
            Object[] obj = list.get(j);
            for (int i = 0; i < obj.length; i++) {
            	cell = bodyRow.createCell(i);
                cell.setCellStyle(bodyStyle);
                cell.setCellValue(obj[i].toString());
			}
        }
        
        String fileName = "zyz_"+DateUtil.dataNowShot();
        String downpath = "";
        // 系统平台
  		String platform = System.getProperty("os.name","No Define System name!");
  		// 判断平台
  		if (platform.toLowerCase().indexOf("windows") != -1) {
  			downpath = "d:\\zyz\\";
  		} else {
  			downpath = "/opt/zyz/download/";
  		}
        FileOutputStream out = new FileOutputStream(downpath+fileName+".xlsx");
        try {
            workBook.write(out);
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
            	out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
 		File dpath = new File(downpath);
		if (!dpath.exists()){
			dpath.mkdirs();
		}
		downpath = downpath + fileName + ".xlsx";
		dpath = new File(downpath);
		response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition","attachment; filename=\""+ downpath.substring(downpath.lastIndexOf('/') + 1) + "\"");
		int i = 0;
		byte[] b = new byte[1024];
 		FileInputStream inputStream = null;
 		OutputStream outputStream = null;
 		try {
			inputStream = new FileInputStream(dpath);
			outputStream = response.getOutputStream();
			while ((i = inputStream.read(b)) > 0) {
				outputStream.write(b, 0, i);
			}
			inputStream.close();
			outputStream.flush();
			outputStream.close();
		} catch (Exception e) {
			
		}
    }
}
